package leecode;

import org.junit.Test;

import java.util.*;

public class Question837 {

    public int totalFruit(int[] fruits) {
        int len = fruits.length;
        int max = 0;
        int count;
        int fA, fB;
        for (int i = 0; i < len; i++) {
            int j = i;
            fA = fruits[i];
            fB = -1;
            count = 0;
            for (; j < len; j++) {
                if (fB == -1) {
                    count++;
                    if (fruits[j] == fA) {
                        i = j;
                    } else {
                        fB = fruits[j];
                    }
                } else if (fruits[j] == fB || fruits[j] == fA) {
                    count++;
                } else {
                    max = count > max ? count : max;
                    break;
                }
            }
            if (j == len) {
                max = count > max ? count : max;
                break;
            }
        }
        return max;
    }
    public int[] nextLargerNodes(ListNode head) {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        while (head != null) {
            map.put(head.val, 0);
            head = head.next;
            for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
                if (entry.getKey() > entry.getValue() && head.val > entry.getKey()) {
                    map.put(entry.getKey(), head.val);
                }
            }
        }
        int[] ans = new int[map.size()];
        int idx = 0;
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            ans[idx] = entry.getValue();
            idx++;
        }
        return ans;
    }
    @Test
    public void main(){
        int[] fruits = {1,2,1};
        System.out.println(totalFruit(fruits));
    }

      public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

}
